package thirt.view
{
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Rectangle;
	import flash.net.SharedObject;
	import flash.text.TextField;
	
	import mx.core.Application;
	
	import thirt.component.TextSprite;
	
	public class Console extends Sprite
	{
		[Embed(source="style/images/move.png")]
		private var moveCls:Class;
		[Embed(source="style/images/rotation.png")]
		private var rotationCls:Class;
		[Embed(source="style/images/scale.png")]
		private var scaleCls:Class;
		[Embed(source="style/images/del.png")]
		private var delCls:Class;
		[Embed(source="style/images/wChange.png")]
		private var wChangeCls:Class;
		
		private var first:ConsolePoint;
		private var second:ConsolePoint;
		private var thrid:ConsolePoint;
		private var fourth:ConsolePoint;
		private var target:ConsolePoint;
		private var type:String;
		public var targets:DisplayObject;
		private var coefficient:Number;
		public var change:Boolean = true;
		public var content:Sprite;
		private var _rec:Rectangle;
		public var isShowRatotion:Boolean;
		public var isScale:Boolean;
		private var _select:Boolean;
		private var maskTarget:DisplayObject;
		public var url:Object;
		public var orgScale:Number;
		public var single:Boolean;
		public var data:Object;
		public var editMovie:Boolean;
		
		private var lineShape:Shape = new Shape();
		public var ishot:int;
		public function Console(targets:DisplayObject,isShowRatotion:Boolean = true,url:Object=null,isscale:Boolean=true)
		{
			this.isShowRatotion = isShowRatotion;
			this.isScale = isscale;
			this.url = url;
			content = new Sprite();
			this.addChild(content);
			this.targets = targets;
			addBitmap();
			content.addChild(lineShape);
			content.addEventListener(MouseEvent.MOUSE_DOWN,onContentMouseClick);
			addPoint()
			drawLine();
			targets.addEventListener(Event.CHANGE,onChange);
		}
		
		private function onChange(event:Event):void{
			targets.x = - targets.width * 0.5;
			targets.y =  - targets.height * 0.5;
			coefficient = targets.width*0.5;
			first.x = targets.x;
			first.y = targets.y;
			second.x = targets.x+targets.width;
			second.y = targets.y;
			thrid.x = targets.x+targets.width;
			thrid.y = targets.y+targets.height;
			fourth.x = targets.x;
			fourth.y = targets.y+targets.height;
			drawLine();
		}
		
		private function addPoint():void{
			first = new ConsolePoint(content,targets.x,targets.y,onClick,onMosueOver,moveCls);
			if(this.isShowRatotion)
				second = new ConsolePoint(content,targets.x+targets.width,targets.y,onClick,onMosueOver,rotationCls);
			else
				second = new ConsolePoint(content,targets.x+targets.width,targets.y,onClick,onMosueOver,wChangeCls);
			thrid = new ConsolePoint(content,targets.x+targets.width,targets.y+targets.height,onClick,onMosueOver,scaleCls);
			fourth = new ConsolePoint(content,targets.x,targets.y+targets.height,onClick,onMosueOver,delCls);
			fourth.addEventListener(MouseEvent.CLICK,onDel);
			first.addText();
			/* if(thrid.visible)
				thrid.visible = this.isScale; */
		}
		private function addBitmap():void{
			content.addChild(targets);
			targets.x = - targets.width * 0.5;
			targets.y =  - targets.height * 0.5;
			coefficient = targets.width*0.5;
		}
		private function onMosueOver(event:MouseEvent):void{
			
		}
		private function onContentMouseClick(event:MouseEvent):void{
			if(event.target is ConsolePoint){
				return;
			}
			type = "move";
			change = false;
			this.startDrag();
			stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
			stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
		}
		private function onClick(event:MouseEvent):void{
			if(event.target == thrid){
				type = "scale"
			}else if(event.target == second){
				if(isShowRatotion == false){
					type = "chgW"
				}else{
					type = "rotation"
				}
				
			}else if(event.target == first){
				type = "move";
				this.startDrag();
			} 
			hideOther(ConsolePoint(event.target));
			change = false;
			target = event.target as ConsolePoint;
			stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
			stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
		}
		private function hideOther(tag:ConsolePoint):void{
			if(first != tag){
				first.visible = false;
			}
			if(second != tag){
				second.visible = false;
			}
			if(thrid != tag){
				thrid.visible = false;
			}
			if(fourth != tag){
				fourth.visible = false;
			}
		}
		private function showAll():void{
			if(this.select == false){
				return;
			}
			first.visible = true;
			second.visible = true;
			thrid.visible = true;
			fourth.visible = true;
			//thrid.visible = this.isScale;
			/* if(thrid.visible)
				thrid.visible = this.isScale; */
		}
		private function onMouseMove(event:MouseEvent):void{
			if(type == "scale")
				runNewPoint();
			else if(type == "rotation")
				runRotation();
			else if(type == "chgW")
				runChgWidth();
			drawLine();
			
		}
		private function onMouseUp(event:MouseEvent):void{
			if(stage == null){
				return;
			}
			stage.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
			this.stopDrag();
			change = true;
			showAll();
		}
		
		public function runChgWidth():void{
			
			TextSprite(targets).textWidth = content.mouseX * 2 // targets.scaleX ;
			
			targets.x = - targets.width * 0.5;
			targets.y =  - targets.height * 0.5;
			first.setXY(targets.x,targets.y);
			second.setXY(targets.x+targets.width,targets.y);
			thrid.setXY(targets.x+targets.width,targets.y+targets.height);
			fourth.setXY(targets.x,targets.y+targets.height);
			
		}
		
		public function runNewPoint(sc:Number=-1):void{
			if(targets is TextSprite){
				var dis:int = Math.sqrt(content.mouseY*content.mouseY + content.mouseX*content.mouseX);
				TextSprite(targets).size = dis/5;
				runChgWidth();
				return;
			} 
			if(sc == -1){
				if(!this.isScale && Math.abs(content.mouseX)/coefficient >= 1){
					var share:SharedObject = SharedObject.getLocal("showScale");
					//share.clear();
					if(!share.data.tip){
						Application.application.showScaleTip();
					}
					return;
				}
				targets.scaleX = targets.scaleY = Math.abs(content.mouseX)/coefficient;
			}else{
				targets.scaleX = targets.scaleY = sc;
			}
			trace(content.mouseX,coefficient)
			
			targets.x = - targets.width * 0.5;
			targets.y =  - targets.height * 0.5;
			first.setXY(targets.x,targets.y);
			second.setXY(targets.x+targets.width,targets.y);
			thrid.setXY(targets.x+targets.width,targets.y+targets.height);
			fourth.setXY(targets.x,targets.y+targets.height);
			if(sc != -1){
				drawLine();
			}
		}
		
		private function runRotation():void{
			if(target == second){
				var sx:Number = (this.mouseX-content.x);
				if(sx < 0){
					sx = -sx;
				}
				var angle:Number = Math.atan((this.mouseY-content.y)/(this.mouseX-content.x))
				var angle2:Number =  Math.atan(targets.height/targets.width);
				if((this.mouseX-content.x) < 0){
					angle = Math.PI+angle;
				}
				angle = angle + angle2;
			}
			
			content.rotation = angle * 180/Math.PI;
			first.rotation = second.rotation = thrid.rotation = fourth.rotation = -content.rotation;
		}
		public function setContentRotation(value:Number):void{
			content.rotation = value;
			first.rotation = second.rotation = thrid.rotation = fourth.rotation = -content.rotation;
		}
		private function drawThis():void{
			graphics.beginFill(0x666666,1);
			graphics.drawRect(0,0,763,10);
			graphics.endFill();
		}
		public function drawLine():void{
			
			lineShape.graphics.clear();
			lineShape.graphics.lineStyle(1,0x666666);
			lineShape.graphics.moveTo(first.x,first.y);
			lineShape.graphics.lineTo(second.x,second.y);
			lineShape.graphics.lineTo(thrid.x,thrid.y);
			lineShape.graphics.lineTo(fourth.x,fourth.y);
			lineShape.graphics.lineTo(first.x,first.y);
			
			if(this.parent == null){
				return;
			}
			var rec:Rectangle = targets.getBounds(this.parent);
			var rec2:Rectangle = EditSpace.getInstance().editRec;
			//var sprite:Sprite = Sprite(EditSpace.getInstance().ui2);
			/* sprite.graphics.clear();
			sprite.graphics.lineStyle(1,0xff0000);
			sprite.graphics.drawRect(rec.x,rec.y,rec.width,rec.height); */
			if(EditSpace.getInstance().editRec.containsRect(rec)){
				first.hideAlert();
				targets.filters = null;
			}else{
				first.showAlert();
				//targets.filters = [new GlowFilter(0x7BB2CC,1,10,10,3)]
				targets.filters = [new GlowFilter(0xff0000,1,10,10,3)]
			}
			//EditSpace.getInstance().onKey();
		}

		public function get select():Boolean
		{
			return _select;
		}

		public function set select(value:Boolean):void
		{
			_select = value;
			lineShape.visible = value;
			first.visible = value;
			second.visible = value;
			thrid.visible = value;
			fourth.visible = value;
			
			if(!value && this.targets is TextSprite){
				TextSprite(targets).removeFouse();
			}
			
			if(value && this.isShowRatotion && this.isScale){
				ChangePanle.getInstance().show(this);
			}
			
			if(value && !this.isShowRatotion && this.isScale){
				ChangeTxtPanle.getInstance().show(this);
				TextSprite(targets).setFouse();
			}
			/* if(value && !this.isScale){
				trace(123)
			} */
			/* if(thrid.visible)
				thrid.visible = this.isScale; */
		}
		
		private var delSprite:Sprite
		public function addDel():void{
			delSprite = new Sprite;
			var txt:TextField = new TextField;
			txt.width = 40;
			txt.height = 20;
			txt.htmlText = "<p align='center'><a href='event:myevent'><font color='#545454'>删除</font></a></p>";
			delSprite.addChild(txt);
			delSprite.graphics.beginFill(0xd3d3d3);
			delSprite.graphics.drawRoundRect(0,0,40,20,5,5);
			delSprite.graphics.endFill();
			this.addChild(delSprite);
			delSprite.visible = false;
			txt.addEventListener(MouseEvent.CLICK,onDel);
			this.addEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
			content.addEventListener(MouseEvent.CLICK,onContentClick);
		}
		public function onDel(event:MouseEvent=null):void{
			if(this.parent){
				this.parent.removeChild(this);
			}
			if(this.isShowRatotion){
				ChangePanle.getInstance().visible = false;
			}else{
				ChangeTxtPanle.getInstance().visible = false;
			}
			var ary:Array = EditSpace.getInstance().currentConsoleAry();
			var index:int = ary.indexOf(this);
			ary.splice(index,1);
			EditSpace.getInstance().setFaceNum();
		}
		private function onMouseOver(event:MouseEvent):void{
			delSprite.x = this.content.width/2;
			delSprite.y = this.content.height/2;
			delSprite.visible = true;
		}
		public function hideDel():void{
			if(delSprite)
				delSprite.visible = false;
		}
		private function onContentClick(event:MouseEvent):void{
			delSprite.visible = false;
		}
		
		public function getAllInfo():Object{
			var obj:Object = new Object;
			obj.x = this.x;
			obj.y = this.y;
			if(targets is MovieClip){
				obj.type = "movie";
				obj.rotation = content.rotation;
				obj.scale = targets.scaleX;
				obj.color = data.color;
				obj.url = data.url;
				obj.id = data.id;
				if(this.editMovie){
					obj.edit = 1;
				}else{
					obj.edit = 0;
				}
					
			}else if(targets is Bitmap){
				obj.type = "img";
				obj.rotation = content.rotation;
				obj.url = data.url;
				obj.baseScale = data.baseScale;
				obj.scale = targets.scaleX;
			}else if(targets is TextSprite){
				obj.type = "text";
				obj.scale = targets.scaleX;
				obj.info = TextSprite(targets).getInfo();
			}
			/* var obj:Object = new Object;
			obj.x = this.x;
			obj.y = this.y;
			if(targets is TextSprite){
				obj.type = 1;
				obj.size = TextSprite(targets).size;
				obj.info = TextSprite(targets).getStr();
			}else{
				obj.type = 2;
				obj.rotation = content.rotation;
				obj.scale = targets.scaleX;
				if(url is String){
					obj.url = url;
				}else if(url is EditItem){
					obj.url = EditItem(url).uploadUrl;
				}
			}
			return obj; */
			return obj;
		}
		
		public function getAllInfoString():String{
			/* var str:String = '';
			str += this.x + "^";
			str += this.y + "^";
			if(targets is TextSprite){
				str += "1^"
				str += TextSprite(targets).size + "^"//size
				str += TextSprite(targets).getStr()//info
			}else{
				str += "2^"
				str += int(content.rotation) + "^"//rotation
				str += targets.scaleX + "^"//scale
				if(url is String){
					str += url + "^"//url
				}else if(url is EditItem){
					str += EditItem(url).bigImgUrl + "^"
				}
				str += this.ishot;
			}http://zhidao.baidu.com/question/170449013.html
			return str; */
			return null;
		}
		
	}
}